//
//  KABExhBuyVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/14.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABExhBuyVC.h"
#import "GlobalHeader.h"
#import "UIColor+XDHex.h"
#import "KABExhBuyTicketTCell.h"
#import "MBProgressHUD+XDPrompt.h"
#import "KABModel.h"
#import "XDAFNetworking.h"
#import "ApiManager.h"
#import <YYWebImage.h>

@interface KABExhBuyVC ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *emptyBtn;
@property (weak, nonatomic) IBOutlet UIImageView *productImgView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *stockBtn;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *cntBgView;
@property (weak, nonatomic) IBOutlet UILabel *describeLabel;
@property (weak, nonatomic) IBOutlet UIButton *decreaseBtn;
@property (weak, nonatomic) IBOutlet UITextField *cntTextField;
@property (weak, nonatomic) IBOutlet UIButton *increaseBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

static NSString *const kReuseCellId = @"KABExhBuyTicketTCell";

@implementation KABExhBuyVC

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    
}

- (void)initSubViews {
    [self.emptyBtn addTarget:self action:@selector(closeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.closeBtn addTarget:self action:@selector(closeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.decreaseBtn addTarget:self action:@selector(decreaseBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.increaseBtn addTarget:self action:@selector(increaseBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.cntBgView.layer.borderWidth = 0.5;
    self.cntBgView.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorBtnBorderColor].CGColor;
    self.cntTextField.layer.borderWidth = 0.5;
    self.cntTextField.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorBtnBorderColor].CGColor;
    
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:kReuseCellId];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 56.0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = CGRectGetHeight(self.sureBtn.bounds) / 2.0;
    [self.sureBtn addTarget:self action:@selector(sureBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [self refreshWholeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.exhItem.exhibition_ticket_list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KABExhBuyTicketTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    KABExhDetTicketItem *item = self.exhItem.exhibition_ticket_list[indexPath.row];
    BOOL isSel = (self.exhItem.ticket_id == item.ticket_id);
    [cell configViewWithDetTicketItem:item isSel:isSel];
    return cell;
}

#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 0.1;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 5.9;
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    KABExhDetTicketItem *item = self.exhItem.exhibition_ticket_list[indexPath.row];
    self.exhItem.ticket_id = item.ticket_id;
    [self refreshWholeView];
}

#pragma mark - event response
- (void)closeBtnTapped:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)decreaseBtnTapped:(UIButton *)btn {
    if (self.exhItem.number > 1) {
        self.exhItem.number--;
        [self refreshWholeView];
    } else {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"不能更少了喔"];
    }
}
- (void)increaseBtnTapped:(UIButton *)btn {
    if (self.exhItem.number < self.exhItem.selMaxTicketCnt) {
        self.exhItem.number++;
        [self refreshWholeView];
    } else {
        NSString *str = [NSString stringWithFormat:@"最多只能购买%zd张", self.exhItem.selMaxTicketCnt];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:str];
    }
}
- (void)sureBtnTapped:(UIButton *)btn {
    if (!self.exhItem.ticket_id) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"请先选择门票类型"];
        return;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.completeBlock) {
            self.completeBlock();
            self.completeBlock = nil;
        }
    }];
}
#pragma mark - private methods
- (void)refreshWholeView {
    [self.productImgView yy_setImageWithURL:[NSURL URLWithString:self.exhItem.cover_image_url] placeholder:[KABDefaultVarModel sharedInstance].smallDefaultImg];
    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2f", self.exhItem.selOriginalPrice / 100.0];
    self.timeLabel.text = [NSString stringWithFormat:@"截至：%@", self.exhItem.end_time];
    
    NSString *stockStr = [NSString stringWithFormat:@"电子票 剩余:%zd张", self.exhItem.selStock];
    [self.stockBtn setTitle:stockStr forState:UIControlStateNormal];
    
    self.describeLabel.text = [NSString stringWithFormat:@"最多购买%zd张", self.exhItem.selMaxTicketCnt];
    self.cntTextField.text = [NSString stringWithFormat:@"%zd", self.exhItem.number];
    
    [self.tableView reloadData];
}

#pragma mark - network request
#pragma mark - getters and setters

@end
